package br.com.felix.fwt.log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;


public class LoggerFactory {
	
	private static String log4jpath;
	
	public enum LoggingMethod {
		LOG4J, WEBLOGIC_LOG4J, FELIX_LOG4J, CONSOLE
	};
	
	
	private static boolean log4jconfigured = false;
	
	private static LoggingMethod loggingMethod = LoggingMethod.LOG4J;
	
	
	
	public static void setLog4JPath(File path){
		if(path == null){
			log4jpath = null;
			setLoggingMethod(LoggingMethod.CONSOLE);
		}
		else{
			log4jpath = path.getAbsolutePath();
		}
	}
	


	public static void setLoggingMethod(LoggingMethod method) {
		loggingMethod = method;
	}
	


	@SuppressWarnings("unchecked")
	public static Logger getLogger(Class clazz) {
		switch (loggingMethod) {
			case WEBLOGIC_LOG4J:
			case LOG4J:
				if (!log4jconfigured) {
					log4jconfigured = true;
					//TODO FIX
					if (log4jpath!=null){
						org.apache.log4j.PropertyConfigurator.configure(log4jpath);
					} 
					else {
						InputStream is = LoggerFactory.class.getClassLoader().getResourceAsStream("log4j.properties");
						if (is!=null){
							org.apache.log4j.PropertyConfigurator.configure(is);
							try {
								is.close();
							}
							catch (IOException e) {
								e.printStackTrace();
								log4jconfigured = false;
							}
						}
					}
				}
				return Logger.getLogger(clazz);
			case FELIX_LOG4J:
				return FelixLogger4J.getLogger(clazz);
			case CONSOLE:
				return FelixConsoleLogger.getLogger(clazz);
		}
		return null;
	}
}
